package com.fbinggun.fbinggunutil;

import com.fbinggun.sql.MySql;
import com.fbinggun.sql.SQLite;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.SQLException;

/**
 * 数据库链接 常用方法
 */
@Deprecated
public class SqlUtil {

    //数据库链接类 需要在插件启动方法 使用SQLite sql = reloadSQL();

    /**
     * 创建 SQLite 数据库链接类
     *
     * 配置文件:
     * SQL:
     *   MySQLEnable: true
     *   ##用户名
     *   username: root
     *   ##密码
     *   password: root
     *   ##数据库链接地址
     *   hostname: localhost:3306
     *   ##链接到的库
     *   database: ceshi1
     *   table: chenghao
     *   additional: "useSSL=true&useUnicode=true&connectTimeout=43200000"
     *
     * @param plugin 插件
     * @param configurationSection 配置文件
     * @return 数据库链接类
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static SQLite reloadSQL(JavaPlugin plugin, ConfigurationSection configurationSection) throws SQLException, ClassNotFoundException {
        boolean enable = configurationSection.getBoolean("MySQLEnable");
        String username = configurationSection.getString("username");
        String password = configurationSection.getString("password");
        String hostname = configurationSection.getString("hostname");
        String table= configurationSection.getString("table");
        String database = configurationSection.getString("database");
        String additional = configurationSection.getString("additional");
        if(enable){
            if(additional!=null){
                return new MySql(hostname,username,password,database,additional,plugin);
            }
            return new MySql(hostname,username,password,database,false,plugin);
        }else {
            return new SQLite(plugin,table+".db");
        }

    }
}
